package com.cqan.system.rest.impl;

import static com.cqan.common.Constant.getHeader;
import static com.cqan.util.http.HttpStatus.BAD_REQUEST;
import static com.cqan.util.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static com.cqan.util.http.HttpStatus.NO_CONTENT;
import static com.cqan.util.http.HttpStatus.OK;

import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.core.HttpHeaders;

import org.springframework.stereotype.Component;

import com.cqan.account.entity.User;
import com.cqan.account.service.UserService;
import com.cqan.log.entity.GenericLog;
import com.cqan.log.service.GenericLogService;
import com.cqan.orm.PropertyFilter;
import com.cqan.orm.PropertyFilter.MatchType;
import com.cqan.service.rest.BaseRestService;
import com.cqan.system.entity.SystemConfig;
import com.cqan.system.entity.SystemConfig.ConfigType;
import com.cqan.system.entity.VersionUpgrade;
import com.cqan.system.entity.resp.ServiceInfoResp;
import com.cqan.system.entity.resp.ServiceResultResp;
import com.cqan.system.entity.resp.VersionUpgradetResp;
import com.cqan.system.rest.SystemRestService;
import com.cqan.system.service.SystemConfigService;
import com.cqan.system.service.VersionUpgradeService;
import com.google.common.collect.Lists;

@Component("systemRestService")
public class SystemRestServiceImpl extends BaseRestService  implements SystemRestService{

	@Resource(name="versionUpgradeService")
	private VersionUpgradeService versionUpgradeService;
	
	@Resource(name="systemConfigService")
	private SystemConfigService systemConfigService;
	
	@Resource(name="userService")
	private UserService userService;
	
	@Resource(name="genericLogService")
	private GenericLogService genericLogService;
	
	@Override
	public VersionUpgradetResp upgrade(HttpHeaders headers) {
		VersionUpgradetResp resp = new VersionUpgradetResp();
		try {
			String ab = getHeader(headers).get("ab");
			String cid = getHeader(headers).get("cid");
			if (ab==null||"".equals(ab)||cid==null||"".equals(cid)) {
				resp.setInfocode(BAD_REQUEST);
				return resp;
			}
			List<PropertyFilter> filters = Lists.newArrayList();
			filters.add(new PropertyFilter("EQL_ab", ab));
			filters.add(new PropertyFilter("EQS_cid",cid));
			List<VersionUpgrade> vu = versionUpgradeService.getList(filters);
			if (vu==null) {
				resp.setInfocode(NO_CONTENT);
				return resp;
			}
			if (vu!=null&&vu.size()>0) {
				resp.setVersionUpgrade(vu.get(0));
			}
			resp.setInfocode(OK);
			genericLogService.add(new GenericLog(headers, req.getRemoteAddr(),"upgrade"));
		} catch (Exception e) {
			resp.setInfocode(INTERNAL_SERVER_ERROR);
			e.printStackTrace();
		}
		return resp;
	}

	@Override
	public ServiceResultResp serviceInfo(HttpHeaders headers) {
		ServiceResultResp resp = new ServiceResultResp();
		try {
			String ab = getHeader(headers).get("ab");
			String did = getHeader(headers).get("did");
			if (ab==null||"".equals(ab)||did==null||"".equals(did)) {
				resp.setInfocode(BAD_REQUEST);
				return resp;
			}
			List<User> users = userService.findBy("did", did, MatchType.EQ);
			if (users==null||users.size()==0) {
				addUnRegUser(did);
			}
			List<SystemConfig> configs = systemConfigService.getSystemConfig(Long.parseLong(ab),ConfigType.PRODUCT);
			if (configs==null||configs.size()==0) {
				resp.setInfocode(NO_CONTENT);
				return resp;
			}
			resp.setInfocode(OK);
			genericLogService.add(new GenericLog(headers, req.getRemoteAddr(),"serviceInfo"));
			List<ServiceInfoResp> infos = Lists.newArrayList();
			for (SystemConfig sc : configs) {
				ServiceInfoResp info = new ServiceInfoResp();
				info.setKey(sc.getKey());
				info.setValue(sc.getValue());
				infos.add(info);
			} 
			resp.setService(infos);
		} catch (Exception e) {
			resp.setInfocode(INTERNAL_SERVER_ERROR);
			return resp;
		}
		return resp;
	}

	private void addUnRegUser(String did) {
		User user = new User();
		user.setAddress(null);
		user.setBirthday(Calendar.getInstance().getTime());
		user.setUsername(did);
		user.setCreateTime(Calendar.getInstance().getTime());
		user.setDid(did);
		user.setEnabled(true);
		user.setLastLoginIp(req.getRemoteAddr());
		user.setRegIp(req.getRemoteAddr());
		user.setSpecial(false);
		user.setUserGrade(0);
		userService.add(user);
	}

}
